import type { PaginationResponse } from "types";
import type { User } from "types/user";
import { http } from "@/utils/http";

export type UserResult = {
  /** id */
  id: number;
  /** 头像 */
  avatar?: string;
  /** 用户名 */
  username: string;
  /** 昵称 */
  nickname?: string;
  /** 当前登录用户的角色 */
  roles: Array<string>;
  /** 按钮级别权限 */
  permissions: Array<string>;
  /** `token` */
  access_token: string;
  /** 用于调用刷新`accessToken`的接口时所需的`token` */
  refresh_token: string;
  /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
  expires: number;
};

export type RefreshTokenResult = {
  /** `token` */
  access_token: string;
  /** 用于调用刷新`accessToken`的接口时所需的`token` */
  refresh_token: string;
  /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
  expires: number;
};

/** 登录 */
export const getLogin = (data: { username: string; password: string }) => {
  return http.request<UserResult>("post", "/users/login", {
    data,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

/** 刷新`token` */
export const refreshTokenApi = (data: { refresh_token: string }) => {
  return http.request<RefreshTokenResult>("post", "/users/refresh-token", {
    data
  });
};

export const getUserPagination = (params: {
  page: number;
  page_size: number;
  kw?: string;
}) => {
  return http.request<PaginationResponse<User>>("get", "/users/pagination", {
    params
  });
};

export const registerUser = (data: object) => {
  return http.request<UserResult>("post", "/users/register", {
    data
  });
};

export const adminChangePassword = (data: object) => {
  return http.request("put", "/users/admin-change-password", {
    data
  });
};

export const userChangePassword = (data: object) => {
  return http.request("put", "/users/change-password", {
    data
  });
};

export const changeUserInfo = (data: object) => {
  return http.request("put", "/users/change-info", {
    data
  });
};

export const deleteUser = (id: number) => {
  return http.request("delete", "/users/" + id);
};
